package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.time.Duration;

public class LoginPage extends Utils {
    public static String url = "http://127.0.0.1:58080/sign-in.html";
    public LoginPage() {
        super(url);
    }

    /**
     * 检查页面是否可以正常打开
     */
    public void checkPageRight() throws InterruptedException {
        // 检查菜单是否加载成功
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2"));
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a"));
        // 检查登录框是否加载成功
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));
    }

    /**
     * 异常登录
     * ---用户名和密码都为空
     * ---用户名为空，密码不为空
     * ---用户名不为空，密码为空
     * ---输入正确的账号，错误的密码
     * ---输入错误的账号（不存在），错误的密码
     * ---输入错误的账号，正确的密码
     */
    public void LoginFail (String username,String password) {
        // 账号密码都为空
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit")).click();
        driver.findElement(By.cssSelector("#signInForm > div.mb-3 > div")); // 用户名为空
        driver.findElement(By.cssSelector("#signInForm > div.mb-2 > div > div")); // 密码为空
        // 账号为空
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#submit")).click();
        driver.findElement(By.cssSelector("#signInForm > div.mb-3 > div")); // 用户名为空
        // 清除一下内容
        driver.findElement(By.cssSelector("#password")).clear();
        // 密码为空
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit")).click();
        driver.findElement(By.cssSelector("#signInForm > div.mb-2 > div > div")); // 密码为空
    }

    /**
     * 异常登录
     * 输入不正确的账号和密码, 检查是否会出现图标
     */
    public void LoginFail1 (String username,String password) {
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        // 账号密码有误
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#submit")).click();
        // 捕获错误
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        String text = driver.findElement(By.xpath("/html/body/div[2]/div")).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("用户名或密码错误"); //  // 用户名或密码错误
    }


    /**
     * 登陆成功
     */
    public void LoginSuccess (String username,String password) throws InterruptedException {
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        // 账号密码正确
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#submit")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        // 检查是否出现错误
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post"));
        driver.findElement(By.cssSelector("#index_nav_avatar"));
    }


}
